There are many different types of tasks
that you need to perform when working with a MySQL database. Functions are a great
way to store the syntax that you need for these tasks as reusable blocks of code. These code blocks mean that you don't
have to re-type your code each time you need to use it. Over the next few minutes,
you will recap the basics of functions and learn how they're used with Python. Little Lemons MySQL database holds
a lot of data on different aspects of the company like customer behavior and
sales revenue. Little Lemon can use functions to perform
specific operations on their database and return results. They can then use the data from these
results to improve the restaurant's performance. So, let's quickly recap
the basics of MySQL functions. As you probably already
know a MySQL function is a piece of code that performs a specific
operation and returns a result. In other words, it's a task that
combines a set of instructions and produces results in
the form of an output. MySQL functions provide a lot of
advantages for database engineers. Some MySQL functions accept parameters or
arguments while others don't. They're great for
manipulating data in a database. You can also create custom functions that
combine several tasks in a block of code. You can then store these functions within
your database and invoke them when needed. And as you've just discovered,
MySQL functions are reusable, so they can be used to complete repeat
tasks at this stage of your database engineering journey. You've probably made use of many
different types of functions. The most common built in functions
available in MySQL include string functions, numeric functions and
date and time functions. There's also comparison and
control flow functions. Let's take a moment to recap the basics
of these different types of functions and look at how Little Lemon
can make use of them. Let's begin with string functions. These functions are used to manipulate
string values like adding strings together or extracting a segment of a string. An example of a string function is CONCAT. CONCAT combines data from two
separate tables into one string. Little Lemon can use a CONCAT string query
function on their database records to extract information on each customer
like how much money they spent. You can also use numeric
functions in MySQL. Numeric functions include aggregate and
math functions. These functions are used to carry out
common tasks on numeric data sets. For example, Little Lemon can use
the average numeric function to determine the average dollar amount that each
customer spends with the business. Another example of MySQL functions
includes date and time functions. Date and time functions can be used to
query a MySQL database to extract date and time values in a range of different
formats depending on the query. Over at Little Lemon. They often extract date and time data to
analyze the behavior of their customers. They can use this data to find out how
long guests spend at the restaurant and which days are the busiest. Next up is comparison functions. You can use these functions to compare
values within a database and they can be used with many different types of values
like numerical, strings and characters. Little Lemon make use of comparison
functions to identify the best and lowest selling items on their
menu by using greatest and least comparison functions
on their sales data. And finally there's
control flow functions. Control flow functions are used
to evaluate conditions and to determine the execution path or
flow of a query. For example, as you learned previously, the case function runs through a set
of conditions within a case block. It then returns a value once
the condition is mesh or a null value, if no conditions are met. Little Lemon often rely on control flow
functions to determine which items on their menu are loss making and
which items have turned a profit. Now that you've recapped the different
functions available in MySQL, let's find out how they work with Python. Let's take the example
of numeric functions and see how Little Lemon calculate the mean or
average bill for each customer. They can create a select query that
uses the average function to determine the average bill amount. This query is passed to
the database to be executed. Once the query is executed,
Little Lemon can access the results and see the average dollar amount each
client spent with the business. This is just a basic recap
of MySQL functions and a short demonstration of
how they work in Python. The rest of this lesson
will explore methods for accessing MySQL functions
using Python in more detail. I'm looking forward to exploring
this topic in more detail with you.